/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_cicsts_eci_resp.c                                  */
/*  NOTE      =                                                    */
/*  DATE      = 2016/05/03 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frct_comm.h"

#include <cicsts/config.h>
#include <cicsts/cicsts_core.h>
#include <cicsts/cicsts_comm.h>
#include <cicsts/cicsts_packet.h>
#include <cicsts/cicsts_trans.h>
#include <apmtest_comm.h>
#include <frreport_opt.h>
#include "../test_cicsts_comm.h"

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    L7_cicsts_stmt *p_st_stmt = NULL;
    Frlist_node *p_st_node = NULL;
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_cicsts_create_ct(&p_st_conn, TEST_CICSTS_SRC_IP, TEST_CICSTS_DST_IP, TEST_CICSTS_SRC_PORT, TEST_CICSTS_DST_PORT), FR_SUCC);

    L7_session *p_st_session = frct_get_session(p_st_conn);

    L7session_cicsts_private *p_st_session_private = (L7session_cicsts_private *) frct_get_session_private(p_st_session);
    L7_cicsts_packet *p_st_cicsts_packet = p_st_session_private->p_st_cicsts_packet;

    char *p_bin = (char *) "0000068c0000107b00039001068012f207430e020000000675063c3f786d6c2076657273696f6e3d22312e30223f3e3c54726164653e3c526571756573744865616465723e3c627573696e65737341636365707449643e373630323031363034323033333835323830333c2f627573696e65737341636365707449643e3c73657269616c4e756d6265723e303c2f73657269616c4e756d6265723e3c76657273696f6e3e312e303c2f76657273696f6e3e3c636d64547970653e303c2f636d64547970653e3c7265717565737465723e545359573c2f7265717565737465723e3c6368616e6e656c3e435a42303c2f6368616e6e656c3e3c7265717565737454696d655374616d703e323031362d30342d32302031313a33343a33312e3030303330313c2f7265717565737454696d655374616d703e3c6f7267616e697a6174696f6e556e697449643e3c2f6f7267616e697a6174696f6e556e697449643e3c74656c6c6572313e747379773c2f74656c6c6572313e3c74656c6c6572323e747379773c2f74656c6c6572323e3c7365727669636549643e68715a4858583c2f7365727669636549643e3c2f526571756573744865616465723e3c426f64793e3c7a686c786d3e3231313c2f7a686c786d3e3c7a683e333031303030303031303132303130303032383332383c2f7a683e3c7a6878683e3c2f7a6878683e3c6a676d3e3330313030303030313c2f6a676d3e3c627a646d3e30313c2f627a646d3e3c71786d3e30303c2f71786d3e3c79777a6c6d3e3230313c2f79777a6c6d3e3c7a75683e30323c2f7a75683e3c7a686c623e323c2f7a686c623e3c7a686c783e313c2f7a686c783e3c686d3e746573743132333c2f686d3e3c647a3e746573743c2f647a3e3c64683e746573743c2f64683e3c6b68683e3638323532363335393c2f6b68683e3c79653e3935333836332e30303c2f79653e3c6b7a7979653e3935333836332e30303c2f6b7a7979653e3c716879653e3935333836332e30303c2f716879653e3c7a7279653e3935333836332e30303c2f7a7279653e3c6c786a733e38353433373437302e30303c2f6c786a733e3c7a687a743e30303030303030303c2f7a687a743e3c66646c6c78733e312e303030303c2f66646c6c78733e3c787968627a3e303c2f787968627a3e3c78796a653e302e30303c2f78796a653e3c78797a683e3c2f78797a683e3c78796c6c3e302e303030303c2f78796c6c3e3c78796a733e302e30303c2f78796a733e3c6e646a733e323936363432333933322e33303c2f6e646a733e3c6d7862733e313c2f6d7862733e3c647968733e303c2f647968733e3c776462733e303c2f776462733e3c6b6872713e323030392d30362d31323c2f6b6872713e3c6b686a653e302e30303c2f6b686a653e3c636b717872713e323031352d30362d32313c2f636b717872713e3c7362667372713e323031362d30342d31393c2f7362667372713e3c636b647172713e313839392d31322d33313c2f636b647172713e3c7a7a7163733e303c2f7a7a7163733e3c797a7163733e303c2f797a7163733e3c7478627a3e303c2f7478627a3e3c74646a623e313c2f74646a623e3c7878626d3e323031313131323134303030313232303730313c2f7878626d3e3c6b6d6b7a7a3e3230313030313c2f6b6d6b7a7a3e3c636868627a3e313c2f636868627a3e3c6a78627a3e323c2f6a78627a3e3c747a627a3e303c2f747a627a3e3c6479627a3e303c2f6479627a3e3c637a793e30353030383c2f637a793e3c7371637a793e30353030333c2f7371637a793e3c6c6367793e3c2f6c6367793e3c687a6a626d3e3c2f687a6a626d3e3c6a737462647a683e3c2f6a737462647a683e3c6a73746264627a3e3c2f6a73746264627a3e3c7a7166733e353c2f7a7166733e3c647a66733e313c2f647a66733e3c667a7a683e333031303030303031303132303130303032383332383c2f667a7a683e3c6b686c783e323c2f6b686c783e3c646b6c6c646d3e51595a545e313c2f646b6c6c646d3e3c2f426f64793e3c526573706f6e73654865616465723e3c686f737453657269616c4e6f3e32303039383231373c2f686f737453657269616c4e6f3e3c726573706f6e736554696d655374616d703e323031362d30342d32303c2f726573706f6e736554696d655374616d703e3c6572726f724e6f3e3030303030303c2f6572726f724e6f3e3c6572726f72496e666f3e3c2f6572726f72496e666f3e3c2f526573706f6e73654865616465723e3c736f614d6f7a3e3c2f736f614d6f7a3e3c2f54726164653e";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_CICSTS_SRC_IP, TEST_CICSTS_DST_IP, TEST_CICSTS_SRC_PORT, TEST_CICSTS_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    CU_ASSERT_EQUAL(cicsts_packet_unfragment(p_st_packet, p_st_cicsts_packet), APM_CICSTS_CHECK_FRAGMENT_SUCCESS);

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    p_st_stmt = (L7_cicsts_stmt *) frmalloc(sizeof(L7_cicsts_stmt));
    CU_ASSERT_PTR_NOT_NULL(p_st_stmt);
    memset(p_st_stmt, 0x00, sizeof(L7_cicsts_stmt));
    p_st_stmt->ui_msg_id = p_st_cicsts_packet->ui_msg_id;

    p_st_stmt->p_report_comm = frlist_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_stmt->p_report_comm);
    p_st_stmt->p_report_req = frlist_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_stmt->p_report_req);

    p_st_node = frlist_node_create(p_st_stmt);
    CU_ASSERT_EQUAL(frhash2_addLong(p_st_session_private->p_hash_stmt, p_st_cicsts_packet->ui_msg_id, p_st_node), FR_SUCC);

    CU_ASSERT_EQUAL(cicsts_resp(p_st_conn, p_st_cicsts_packet, &st_hash_report), FR_SUCC);

    Frvar_field *p_st_field = NULL;
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data = NULL;
    p_list_report = &((&st_hash_report)->p_list_array[0]);

    p_st_node_report = FRLIST_LAST_NODE(p_list_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_node_report);

    p_st_report_data = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_report_data);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_DTS_REPORT_RESP_ARG_SIZE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_DTS_REPORT_RESP_ARG_DATA, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRBLOB);
    CU_ASSERT_NSTRING_EQUAL(p_st_field->field_value.p_st_value->p_data, "<?xml version=\"1.0\"?><Trade><RequestHeader><businessAcceptId>7602016042033852803</businessAcceptId><serialNumber>0</serialNumber><version>1.0</version><cmdType>0</cmdType><requester>TSYW</requester><channel>CZB0</channel><requestTimeStamp>2016-04-20 11:34:31.000301</requestTimeStamp><organizationUnitId></organizationUnitId><teller1>tsyw</teller1><teller2>tsyw</teller2><serviceId>hqZHXX</serviceId></RequestHeader><Body><zhlxm>211</zhlxm><zh>3010000010120100028328</zh><zhxh></zhxh><jgm>301000001</jgm><bzdm>01</bzdm><qxm>00</qxm><ywzlm>201</ywzlm><zuh>02</zuh><zhlb>2</zhlb><zhlx>1</zhlx><hm>test123</hm><dz>test</dz><dh>test</dh><khh>682526359</khh><ye>953863.00</ye><kzyye>953863.00</kzyye><qhye>953863.00</qhye><zrye>953863.00</zrye><lxjs>85437470.00</lxjs><zhzt>00000000</zhzt><fdllxs>1.0000</fdllxs><xyhbz>0</xyhbz><xyje>0.00</xyje><xyzh></xyzh><xyll>0.0000</xyll><xyjs>0.00</xyjs><ndjs>2966423932.30</ndjs><mxbs>1</mxbs><dyhs>0</dyhs><wdbs>0</wdbs><khrq>2009-06-12</khrq><khje>0.00</khje><ckqxrq>2015-06-21</ckqxrq><sbfsrq>2016-04-19</sbfsrq><ckdqrq>1899-12-31</ckdqrq><zzqcs>0</zzqcs><yzqcs>0</yzqcs><txbz>0</txbz><tdjb>1</tdjb><xxbm>2011112140001220701</xxbm><kmkzz>201001</kmkzz><chhbz>1</chhbz><jxbz>2</jxbz><tzbz>0</tzbz><dybz>0</dybz><czy>05008</czy><sqczy>05003</sqczy><lcgy></lcgy><hzjbm></hzjbm><jstbdzh></jstbdzh><jstbdbz></jstbdbz><zqfs>5</zqfs><dzfs>1</dzfs><fzzh>3010000010120100028328</fzzh><khlx>2</khlx><dklldm>QYZT^1</dklldm></Body><ResponseHeader><hostSerialNo>20098217</hostSerialNo><responseTimeStamp>2016-04-20</responseTimeStamp><errorNo>000000</errorNo><errorInfo></errorInfo></ResponseHeader><soaMoz></soaMoz></Trade>", 1650);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_START_TIMESTAMP, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1454549354123L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_END_TIMESTAMP, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1454549354123L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_SERVER_TIME_US, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1454549354123456L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_DOWNLOAD_TIME_US, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 0);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_DATA_BYTES, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1676L);

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_cicsts_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_cicsts_resp(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" cicsts_resp()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
